import { Ref, ref } from "vue";

export function PreAndNextBtnEvent() {
  // 左侧上一条,下一条按钮显示与隐藏
  const leftBtnAreaVisible = ref(false);
  //当前数据所在总数据的位置下标
  const dataIndex = ref();
  const previousDisabled = ref();
  const nextDisabled = ref();
  const submitFormData = ref();

  // 点击下一条数据的逻辑判断
  const nextRecord = async (tableData: Ref<never[]>) => {
    if (dataIndex.value == tableData.value.length - 1) {
      nextDisabled.value = true;
    } else {
      dataIndex.value += 1;
      submitFormData.value = JSON.parse(
        JSON.stringify(tableData[dataIndex.value])
      );
      previousDisabled.value = false;
      nextDisabled.value = false;
    }
  };

  // 点击上一条数据的逻辑判断
  const previousRecord = async (tableData: Ref<never[]>) => {
    if (dataIndex.value == 0) {
      previousDisabled.value = true;
    } else {
      dataIndex.value -= 1;
      submitFormData.value = JSON.parse(
        JSON.stringify(tableData[dataIndex.value])
      );
      previousDisabled.value = false;
      nextDisabled.value = false;
    }
  };

  return {
    submitFormData,
    leftBtnAreaVisible,
    dataIndex,
    previousDisabled,
    nextDisabled,
    nextRecord,
    previousRecord,
  };
}
